type MessageType = 'image' | 'test' | string;
type Message = {
  id: number;
  type: MessageType;
  message: string;
};
const msgList: Message[] = Array(20)
  .fill(1)
  .map((d: number, index: number) => ({
    id: index,
    type: Math.random() > 0.5 ? 'image' : 'test',
    message: `message${index}`,
  }));

// 1 定义重载签名函数1
function getMessage(id: number): Message | undefined;
// 2 定义重载签名函数2
function getMessage(type: MessageType): Message[];
// 3 定义实现签名
function getMessage(
  params: number | MessageType,
  count: number = Infinity
): Message | Message[] | undefined {
  if (typeof params === 'number') {
    return msgList.find((data) => data.id === params);
  } else {
    return msgList.filter((msg) => msg.type === params).slice(0, count + 1);
  }
}

console.log(getMessage(1)?.message);
console.log(getMessage('image'));

(function () {
  type Params = {
    width: number;
    height: number;
  };
  class Squ {
    public width: number;
    public height: number;
    constructor(width: number, height: number);
    constructor(params: Params);
    constructor(paramsOrWidth: any, height: number = 0) {
      if (typeof paramsOrWidth === 'object') {
        this.width = paramsOrWidth.width;
        this.height = paramsOrWidth.height;
      } else {
        this.width = paramsOrWidth;
        this.height = height;
      }
    }

    getArea() {
      return this.width * this.height;
    }
  }
  const s = new Squ(10, 10);
  console.log(s.getArea());
  const s1 = new Squ({ width: 10, height: 10 });
  console.log(s1.getArea());
})();
